package cn.breeze.org.service.impl;

import cn.breeze.basic.service.impl.BaseServiceImpl;
import cn.breeze.basic.utils.PageList;
import cn.breeze.org.domain.Department;
import cn.breeze.org.mapper.DepartmentMapper;
import cn.breeze.org.query.DepartmentQuery;
import cn.breeze.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
// 只读事务
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    /** 
    * @Title: remove
    * @Description: TODO
    * @Author: Mr.D
    * @Version: 1.0
    * @Date:  2022/6/21 18:46  
    * @Parameters: [id]
    * @Return 
    */
    @Override
    @Transactional
    public void remove(Long id) {
        Long[] longs = {id};
        List<Department> departments = departmentMapper.loadSonDepartment(longs);
        if (departments != null){
            for (Department department : departments) {
                department.setDirPath("/" + department.getId());
                departmentMapper.update(department);
            }
        }
        departmentMapper.remove(id);
    }

    /**
    * @Title: save
    * @Description: TODO
    * @Author: Mr.She
    * @Version: 1.0
    * @Date:  2022/6/21 18:39
    * @Parameters: [department]
    * @Return void
    */
    @Override
    @Transactional
    public void save(Department department) {
        departmentMapper.save(department);
        String dirPath = "";
        if (department.getParent() == null || department.getParent().getId() == null){
            dirPath = "/" + department.getId();
        } else {
            Department deptPath = departmentMapper.loadById(department.getParent().getId());
            if (deptPath != null){
                dirPath = deptPath.getDirPath() + "/" + department.getId();
            }
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }

    /** 
    * @Title: update
    * @Description: TODO
    * @Author: Mr.She
    * @Version: 1.0
    * @Date:  2022/6/21 18:38  
    * @Parameters: [department]
    * @Return void
    */
    @Override
    @Transactional
    public void update(Department department) {
        String dirPath = "";
        if (department.getParent().getId() == null){
            dirPath = "/" + department.getId();
        } else {
            Department deptPath = departmentMapper.loadById(department.getParent().getId());
            if (deptPath != null){
                dirPath = deptPath.getDirPath() + "/" + department.getId();
                Long[] ids = {department.getId()};
                List<Department> sonDep = departmentMapper.loadSonDepartment(ids);
                if (sonDep != null){
                    for (Department department1 : sonDep) {
                        String sdirPath = dirPath + "/" + department1.getId();
                        department1.setDirPath(sdirPath);
                        departmentMapper.updateDirPath(department1);
                    }
                }
            }
        }
        System.out.println(department);
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }

    @Override
    public void patchRemove(Long[] ids) {
        List<Department> departments = departmentMapper.loadSonDepartment(ids);
        if (departments != null){
            for (Department department : departments) {
                department.setDirPath("/" + department.getId());
                departmentMapper.update(department);
            }
        }
        departmentMapper.patchRemove(ids);
    }

    @Override
    public List<Department> loadDepartmentTree() {
        List<Department> deptAll = departmentMapper.loadAll();
        Map<Long, Department> collect = deptAll.stream().collect(Collectors.toMap(Department::getId, item -> item));
        List<Department> deptList = new LinkedList<Department>();
        for (Department first : deptAll){
            if (first.getParentId() == null){
                deptList.add(first);
            } else {
                Department department = collect.get(first.getParentId());
                if (department != null){
                    department.getChildren().add(first);
                }
            }
        }
        return deptList;
    }
}
